#!/usr/bin/env python

from setuptools import Extension, find_packages, setup, Distribution
# from setuptools.command.build_py import build_py
from distutils.command.build_ext import build_ext
import platform
import os
# import glob
# import sys

sources = ["crfpy/CRFPP_wrap.cxx"
        , "CRFPP/crf_learn.cpp"
        , "CRFPP/crf_test.cpp"
        ]
sources += [
    "CRFPP/libcrfpp.cpp",
    "CRFPP/lbfgs.cpp",
    "CRFPP/param.cpp",
    "CRFPP/encoder.cpp",
    "CRFPP/feature.cpp",
    "CRFPP/feature_cache.cpp",
    "CRFPP/feature_index.cpp",
    "CRFPP/node.cpp",
    "CRFPP/path.cpp",
    "CRFPP/tagger.cpp",
]

includes = [
    'CRFPP'
]

sysstr = platform.system()
if sysstr == 'Linux':
    define_macros = [
        ('HAVE_CONFIG_H', 1),
        # ('PACKAGE', '\"CRF++\"'),
        # ('VERSION', '\"0.59\"'),
        # ('HAVE_PTHREAD_H', None)
    ]
    undef_macros = [
        'HAVE_WINDOWS_H'
    ]
elif sysstr == 'Windows':
    define_macros = [
        ('PACKAGE', '\"CRF++\"'),
        ('VERSION', '\"0.59\"'),
        ('_CRT_SECURE_NO_DEPRECATE', None),
        ('DLL_EXPORT', None),
        ('UNICODE', None),
        ('_UNICODE', None),
        ('HAVE_WINDOWS_H', None),
        ('_WIN32', None),
    ]
    undef_macros = [
        'HAVE_PTHREAD_H', 'HAVE_CONFIG_H'
    ]

copt = {'msvc': ['-O2', '-GA', '-GL', '-Gy', '-Oi', '-Ob2',
                 '-nologo', '-W3', '-EHsc', '-MT', '-wd4244'],
        'unix': ['-O3', '-Wall']
        }
lopt = {'msvc': ['-link', '-OPT:REF', '-OPT:ICF', '-LTCG', '-NXCOMPAT',
                 '-DYNAMICBASE ADVAPI32.LIB'],
        'unix': ['-lpthread', '-lm']
        }


class build_ext_subclass(build_ext):
    def build_extensions(self):
        c = self.compiler.compiler_type
        if c in copt:
            for e in self.extensions:
                e.extra_compile_args = copt[c]
        if c in lopt:
            for e in self.extensions:
                e.extra_link_args = lopt[c]

        build_ext.build_extensions(self)


class BinaryDistribution(Distribution):
    def has_ext_modules(foo):
        return True


print(define_macros)
print(undef_macros)
setup(name="crfpy",
      version='0.0.0.3',
      py_modules=["CRFPP", "crfpy/learn","crfpy/test"],
      description="Python binding for crfpp(CRF++)",
      long_description=open('README.rst').read(),
      long_description_content_type='text/markdown',
      author="Chenliang Wang",
      author_email="chenliangwang08ms@gmail.com",
      url='https://github.com/seifer08ms/crfpy',
      zip_safe=False,
      classifiers=[
          'Development Status :: 3 - Alpha',
          'Operating System :: OS Independent',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: BSD License',
          'Programming Language :: Python',
          'Programming Language :: Python :: Implementation',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.7',
          'Programming Language :: Python :: 3.8',
          'Programming Language :: Python :: 3.9',
          'Topic :: Software Development :: Libraries'
      ],
      python_requires='>=2.7',
      keywords="CRF Wrapper CRF++ crfpp module nlp",
      ext_modules=[Extension("_CRFPP",
                             include_dirs=includes,
                             language='c++',
                             sources=sources,
                             # libraries=["crfpp", "pthread"],
                             libraries=["pthread"],
                             define_macros=define_macros,
                             undef_macros=undef_macros
                             )
                   ],
      entry_points={
          'console_scripts': [
              'crf_learn = crfpy.learn:main',
              'crf_test = crfpy.test:main'
          ]
      },
      cmdclass={'build_ext': build_ext_subclass}
      # include_package_data=True,
      # distclass=BinaryDistribution
      )
